package com.ssm.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

public class BeanTest {

    public static void main(String[] args) {
        ApplicationContext applicationContext =
                new AnnotationConfigApplicationContext(AppConfig.class);
        Student student = applicationContext.getBean(Student.class);
        student.study();
        student.sleep();
        student.study();

        student.getAddress().submit("四川-成都-双流");
        student.getAddress().print();
    }
}

@Configuration
//@Import({Student.class, Address.class})
class AppConfig {
    @Bean
    public Student student() {
        return new Student();
    }
}

@Component
class Student {

    private String name;

    private Integer source;

    private Address address;

    public Student() { }
    public Student(Address address) {
        this.address = address;
    }

    public void study() {
        System.out.println("学习中...");
    }

    public void sleep() {
        System.out.println("休息中...");
    }

    public Address getAddress() {
        return address;
    }
}

@Component
class Address {
    private String province;
    private String city;
    private String region;

    public void submit(String address) {
        String[] split = address.split("-");
        if (split.length >= 1) {
            this.province = split[0];
        }
        if (split.length >= 2) {
            this.city = split[1];
        }
        if (split.length >= 3) {
            this.region = split[2];
        }
    }

    public void print() {
        System.out.println(this.province + "-" + this.city + "-" + this.region);
    }
}